package com.zukxu.items.project.spiderman;

import java.io.*;
import java.net.URL;

/**
 * Description:
 *
 * @author zukxu
 * @date 2020/10/23 0023 21:47
 */
public class DownloadPicFromURL {

	/**
	 * 根据链接url下载图片
	 */
	public void downloadPicture(String urlList, String path, String name) {
		URL url = null;
		try {
			url = new URL(urlList);
			DataInputStream dataInputStream = new DataInputStream(url.openStream());
			File file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}
			file = new File(file + "/" + name);
			FileOutputStream fileOutputStream = new FileOutputStream(file);

			ByteArrayOutputStream output = new ByteArrayOutputStream();

			byte[] buffer = new byte[1024];
			int length;

			while ((length = dataInputStream.read(buffer)) > 0) {
				output.write(buffer, 0, length);
			}
			fileOutputStream.write(output.toByteArray());
			dataInputStream.close();
			fileOutputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

